package com.qf.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.qf.hospital.dto.UserDTO;
import com.qf.hospital.entity.User;
import com.qf.hospital.enums.HospitalErrorEnums;
import com.qf.hospital.service.UserService;
import com.qf.hospital.vo.R;
import com.qf.hospital.vo.UserRoleVO;
import com.qf.hospital.vo.UserVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Description: ${TODO}
 * @Date 2021/10/18 10:01
 */

@RestController
public class UserController {

    private Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    /**
     *  查询全部用户信息
     *  接收参数，也可以自行封装一个DTO接收
     * @param realname 昵称
     * @param loginname 登录名
     * @param address 地址
     * @param phone 手机号
     * @param identity 身份证号
     * @param page 当前页
     * @param limit 每页条数
     * @return
     */
    @GetMapping("/user/selectAllUser")
    public R selectAllUser(String realname, String loginname, String address, String phone,String identity,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer limit){
        //1. 调用sysUserService查询全部用户信息
        PageInfo<User> pageInfo = userService.findAll(realname,loginname,address,phone,identity,page,limit);
        //2. 获取数据总条数
        long total = pageInfo.getTotal();
        //3. 获取展示的数据
        List<User> userList = pageInfo.getList();
        List<UserVO> data = new ArrayList<>();
        for (User user : userList) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            userVO.setPwd("");
            data.add(userVO);
        }

        //4. 响应数据
        return R.ok(total, data);
    }


    @PostMapping("/user/addUser")
    public R addUser(@Valid UserDTO userDTO, BindingResult bindingResult){
        //1. 校验参数
        if (bindingResult.hasErrors()){
            // 参数不合法
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.error("【添加用户】 添加失败,参数不合法 msg = {}",msg);
            return R.error(HospitalErrorEnums.USER_ADD_ERROR.getCode(),msg);
        }
        //2. 封装实体类
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        //3. 调用Service保存
        userService.addUser(user);
        //4. 返回
        return R.ok("添加成功！");
    }


    @GetMapping("/user/initUserRole")
    public R initUserRole(Integer userid){
        //1.参数校验
        if (userid == null || userid <= 0){
            log.info("【分配角色】 用户id不能为空 userid = {} ", userid);
            return R.error(HospitalErrorEnums.USER_SAVE_ROLE_ERROR);
        }

        //2.调用service查询数据
        List<UserRoleVO> data = userService.initUserRole(userid);

        //3.返回
        return R.ok(data);
    }

    @PostMapping("/user/saveUserRole")
    public R saveUserRole(Integer userid,Integer[] ids){
        //1.参数校验
        if (userid == null || userid <= 0){
            log.info("【分配角色信息】 参数不合法！");
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR);
        }
        //2.调用service保存角色
        userService.saveUserRole(userid,ids);
        //3.返回
        return R.ok("分配角色信息成功！");
    }


    @PostMapping("/user/updateUser")
    public R updateUser(@Valid UserDTO userDTO,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            String msg = bindingResult.getFieldError().getDefaultMessage();
            log.error("【修改用户信息】 参数不合法", msg);
            return R.error(HospitalErrorEnums.REQUEST_PARAMETER_ERROR,msg);
        }
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        userService.updateByUserId(user);
        return R.ok("修改信息成功！");
    }

    @PostMapping("/user/resetUserPwd")
    public R resetUserPwd(Integer userid){
        userService.resetPwdByUserId(userid);
        return R.ok("密码修改成功！");
    }

    @PostMapping("/user/deleteUser")
    public R deleteUser(Integer userid){
        userService.deleteByUserId(userid);
        return R.ok("删除成功！");
    }


}
